package net.tngou.http;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.alibaba.druid.sql.parser.LayoutCharacters;

import net.tngou.enums.CookieEnum;
import net.tngou.util.DateUtil;



public class ResponseContext extends HttpServletResponseWrapper implements HttpResponse {

	public ResponseContext(HttpServletResponse response) {
		super(response);
		// TODO Auto-generated constructor stub
	}
	
	 

	
	public void addMCookie(long id,String hkey) 
	{
//		int maxAge = 60*60*24;//24小时
		LocalDate enddate = LocalDate.now().plusDays(1);
		LocalDateTime now = LocalDateTime.now();
		Duration d = DateUtil.between(now, enddate.atTime(0, 0));
		Cookie cookie = new Cookie(CookieEnum.ismtg.toString()+"_"+id, hkey);
	
		cookie.setMaxAge((int)d.toMinutes()*60);
	
		cookie.setPath("/");
//		cookie.setDomain(".yi18.net");//域名cookie
		this.addCookie(cookie);
	
		
	}

	/**
	 * 永久，365天
	 */
	@Override
	public void addAutoLoginCookie(String hkey) 
	{

		Cookie cookie = new Cookie(CookieEnum.access_token.toString(), hkey);
		cookie.setPath("/");
		this.addCookie(cookie);
	
		
	}
	
	/**
	 * 制定时间
	 */
	public void addAutoLoginCookie(int time,String hkey) 
	{
		int maxAge = time;
		Cookie cookie = new Cookie(CookieEnum.access_token.toString(), hkey);
		cookie.setMaxAge(maxAge);
		cookie.setPath("/"); //设置保存的目录，这里要看在什么目录下保存cookie
//		cookie.setDomain(".yi18.net");// 设置域名
		this.addCookie(cookie);
	
		
	}

	@Override
	public void removeAutoLoginCookie() {
		
		Cookie cookie = new Cookie(CookieEnum.access_token.toString(), null);
		cookie.setMaxAge(-1);
		cookie.setPath("/");
		this.addCookie(cookie);
	}



	@Override
	public void addCookie(String key, String value) {
		
		Cookie cookie = new Cookie(key, value);
		cookie.setPath("/");
		this.addCookie(cookie);
		
	}

	
	


}
